package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Material;
import com.ruoyi.system.domain.vo.InventoryVo;
import com.ruoyi.system.service.IMaterialService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Inventory;
import com.ruoyi.system.service.IInventoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 库存Controller
 * 
 * @author 华馨悦
 * @date 2025-01-24
 */
@Controller
@RequestMapping("/system/inventory")
public class InventoryController extends BaseController
{
    private String prefix = "system/inventory";

    @Autowired
    private IInventoryService inventoryService;

    @Resource
    private IMaterialService materialService;

    @RequiresPermissions("system:inventory:view")
    @GetMapping()
    public String inventory(ModelMap modelMap)
    {
        modelMap.put("materials", materialService.selectMaterialList(new Material()));
        return prefix + "/inventory";
    }

    /**
     * 查询库存列表
     */
    @RequiresPermissions("system:inventory:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Inventory inventory)
    {
        startPage();
        List<Inventory> list = inventoryService.selectInventoryList(new Inventory());
        List<InventoryVo> inventoryVos = new ArrayList<>();
        list.forEach(i -> {
            Material material = materialService.selectMaterialById(i.getMaterialId());
            InventoryVo inventoryVo = new InventoryVo();
            BeanUtils.copyProperties(i, inventoryVo);
            inventoryVo.setMaterialName(Optional.ofNullable(material).map(Material::getName).orElse("物料信息已过期或被销毁"));
            inventoryVos.add(inventoryVo);
        });
        return getDataTable(inventoryVos);
    }

    /**
     * 导出库存列表
     */
    @RequiresPermissions("system:inventory:export")
    @Log(title = "库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Inventory inventory)
    {
        List<Inventory> list = inventoryService.selectInventoryList(inventory);
        ExcelUtil<Inventory> util = new ExcelUtil<Inventory>(Inventory.class);
        return util.exportExcel(list, "库存数据");
    }

    /**
     * 新增库存
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<Material> materials = materialService.selectMaterialList(new Material());
        mmap.put("materials", materials);
        return prefix + "/add";
    }

    /**
     * 新增保存库存
     */
    @RequiresPermissions("system:inventory:add")
    @Log(title = "库存", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Inventory inventory)
    {
        inventory.setCreatedTime(DateUtils.getNowDate());
        inventory.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(inventoryService.insertInventory(inventory));
    }

    /**
     * 修改库存
     */
    @RequiresPermissions("system:inventory:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Inventory inventory = inventoryService.selectInventoryById(id);
        mmap.put("inventory", inventory);
        List<Material> materials = materialService.selectMaterialList(new Material());
        mmap.put("materials", materials);
        return prefix + "/edit";
    }

    /**
     * 修改保存库存
     */
    @RequiresPermissions("system:inventory:edit")
    @Log(title = "库存", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Inventory inventory)
    {
        inventory.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(inventoryService.updateInventory(inventory));
    }

    /**
     * 删除库存
     */
    @RequiresPermissions("system:inventory:remove")
    @Log(title = "库存", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(inventoryService.softDeleteInventoryByIds(ids));
    }
}
